package com.nisco.dms.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nisco.dms.entity.Permission;
import com.nisco.dms.service.MenuService;
import com.nisco.dms.service.PermissionService;
import com.nisco.dms.service.ProgramService;
import com.nisco.dms.util.ControllerUtil;
import com.nisco.dms.util.dwz.AjaxObject;
import com.nisco.dms.util.dwz.Page;

@Controller
@RequestMapping(value="/permission")
public class PermissionController
{
	@Autowired
	private PermissionService permissionService;
	@Autowired
	private MenuService moduleService;
	@Autowired
	private ProgramService programService;
	private static final String LAYOUTLIST="permission/permissionList";
	private static final String MODULESTREE="permission/selectModule";
	private static final String CREATE="permission/permissionCreate";
	private static final String UPDATE="permission/permissionUpdate";
	private static String  moduleListNavId = "";
	@RequestMapping(value="/list")
	public String layoutList(Page page,Model model,@RequestParam(value="moduleId",required=false) String moduleId,HttpServletRequest request)
	{
		moduleListNavId = ControllerUtil.navId(moduleListNavId, (String)request.getParameter("moduleListNavId"));
		List<Permission> plist=new ArrayList<Permission>();
		if(moduleId!=null&&!"".endsWith(moduleId))
		{
			String moudleName = programService.findOne(moduleId) == null?moduleService.findOne(moduleId).getStrName(): programService.findOne(moduleId).getStrName();
			plist=permissionService.findPermissionsByModuleId(moduleId,page);
			model.addAttribute("moduleId", moduleId);
			model.addAttribute("moduleName", moudleName);
			model.addAttribute("partFresh", true);
		}
		else
		{
			plist=permissionService.findAll(page);
		}
		model.addAttribute("plist", plist);
		return LAYOUTLIST;
	}
	@RequestMapping(value="/getModulesTree")
	public String getModulesTree(Map<String,Object> map)
	{
		map.put("module", moduleService.getTree());
		return MODULESTREE;
	}
	@RequestMapping(value="/preCreate/{module_id}")
	public String preCreate(@PathVariable(value="module_id") String module_id,Model model)
	{
		model.addAttribute("selectedModuleId",module_id);
		return CREATE;
	}
	@RequestMapping(value="/preUpdate/{permissionid}")
	public String preUpdate(@PathVariable(value="permissionid")String permissionid,Model model)
	{
		model.addAttribute("permission", permissionService.get(permissionid));
		return UPDATE;
	}
	@RequestMapping(value="/update",method = RequestMethod.POST)
	public @ResponseBody String update(Permission permission)
	{
		permissionService.update(permission);
		AjaxObject ajaxObject = new AjaxObject("权限更新成功！");
		ajaxObject.setRel("selectModuleDiv");
		ajaxObject.setNavTabId(moduleListNavId);
		ajaxObject.setCallbackType("");
		return ajaxObject.toString();
	}
	@RequestMapping(value="/create",method = RequestMethod.POST)
	public @ResponseBody String create(Permission permission)
	{
		permissionService.save(permission);
		AjaxObject ajaxObject = new AjaxObject("权限添加成功！");
		ajaxObject.setRel("selectModuleDiv");
		return ajaxObject.toString();
	}
	@RequestMapping(value="/delete/{permissionId}",method = RequestMethod.POST)
	public @ResponseBody String delete(@PathVariable(value="permissionId")String permissionId)
	{
		permissionService.delete(permissionId);
		AjaxObject ajaxObject = new AjaxObject("权限删除成功！");
		ajaxObject.setCallbackType("");
		ajaxObject.setRel("selectModuleDiv");
		ajaxObject.setNavTabId(moduleListNavId);
		return ajaxObject.toString();
	}
	
}
